<?php
class MemberAction extends Action{
	public function check(){
		$sname=session('euname');
		if(!isset($sname)) {
			$this->redirect('Member/login');
		}
	}

    //qq登陆
	public function qqlogin(){
		Vendor("qquc.API.qqConnectAPI");
		$qc = new QC();
		$ret=$qc->qq_login();
	}
    //qq登陆回调
	public function qqcall_back(){
		Vendor("qquc.API.qqConnectAPI");
		$qc = new QC();
		$bid=$qc->qq_callback();
		$openid=$qc->get_openid();
		session("token",$bid);
		session("openid",$openid);
        $qc = new QC(session("token"),session("openid"));
		$ret = $qc->get_user_info();
echo session("token")."-------".session("openid");
		if($ret['ret'] == 0){
			$_SESSION['qquser']=$ret;
			$_SESSION['openid']=$openid;
			$this->redirect('Member/qquc');
		}else{
			$this->redirect('Member/qqlogin');
		}
	}

	public function qquc(){
		$Member=M('Member');
		$map['openid']=$_SESSION['openid'];
		$member=$Member->where($map)->find();
		if(empty($map['openid'])){
			$this->redirect('Member/qqlogin');
		}

		if($member){
			session('euname',$member['uname']);
			$data['last_login']=time();
			$Member->where($map)->save($data);
			$Member->where($map)->setInc('login_count');
			session('openid',null); 
			$this->redirect('Index');
		}else{
			$qqinfo=$_SESSION['qquser'];
			$data['uname']=$this->setname($qqinfo['nickname']);
			$data['sex']=($qqinfo['gender']=='男')?'m':'w';
			$data['openid']=$_SESSION['openid'];
			$data['refer']='QQ';
			$data['reg_ip']=get_client_ip();
			$data['u_pic']=$qqinfo['figureurl_1'];
			$data['password']=md5($map['openid']);
			$data['regtime']=time();
			$data['last_login']=time();
			$data['login_count']=1;
			$new=$Member->add($data);
			if($new){
			    session('qquser',null); 
				session('euname',$data['uname']);
				$this->name="欢迎你【{$qqinfo['nickname']}】-蚂蚁街";
				$shop_cart=cookie('shopcart');
				$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
				$this->user=$_SESSION['euname'];
				$this->cat=R('Goods/goods_cat');
			    $this->display();
			}else{
				$this->redirect('Member/qqlogin');
			}
		}
		        
	}

	public function setname($name){
		$Member=M('Member');
		for($i=0;$i<10;$i++){
			$map['uname']=$name;
			if($Member->where($map)->find()){
				$name=$name.'_'.rand(1,1000);
			}else{
				break;
			}
		}
		return $name;
	}
    //qq登陆成功后保存补充信息
	public function setqquc(){
		$Member=M('Member');
		$map2['email']=$this->_post('email');
		if($Member->where($map2)->find()){
			echo 1;
		}else{
		    $map['uname']=$_SESSION['euname'];
			$data['email']=$map2['email'];
			$data['password']=md5($this->_post('pwd'));
			if($Member->where($map)->save($data)){
				echo 2;
			}else{
				echo 3;
			}
		}
	}

	public function index(){
		$this->redirect('trade');
	}

    //个人信息
	public function member_info(){
		$this->check();
		$Member=M('Member');
		$action=$this->_get('action');
		if($action=='save'){
			$member_id=$this->_post('member_id');
			$map['member_id']=array('neq',$member_id);
			$map['email']=$this->_post('email');
			if($Member->where($map)->select()){
				echo 1;
			}else{
				$map2['member_id']=$member_id;
				$data['email']=$map['email'];
				$data['name']=$this->_post('name');
				$data['tel']=$this->_post('tel');
				$data['area']=$this->_post('area');
				$data['sex']=$this->_post('sex');
				$data['mobile']=$this->_post('mobile');
				$Member->where($map2)->save($data);
				echo 2;
			}
		}else{
			$map['uname']=$_SESSION['euname'];
			$this->member=$Member->where($map)->find();

			$this->user=$map['uname'];
			$this->cat=R('Goods/goods_cat');
			$shop_cart=cookie('shopcart');
			$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
			$this->name="会员中心-".$map['uname'];
			$this->display();
		}
	}

    //修改密码
	public function change_pwd(){
		$this->check();
		$Member=M('Member');
		$action=$this->_get('action');
		if($action=='save'){
			$map['member_id']=$this->_post('member_id');
			$map['password']=md5($this->_post('old_pwd'));
			if($Member->where($map)->find()){
				$data['password']=md5($this->_post('new_pwd'));
				$Member->where($map)->save($data);
				echo 2;
			}else{
				echo 1;
			}
		}else{
			$map['uname']=$_SESSION['euname'];
			$this->member=$Member->where($map)->find();

			$this->user=$map['uname'];
			$this->cat=R('Goods/goods_cat');
			$shop_cart=cookie('shopcart');
			$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
			$this->name="会员中心-".$map['uname'];
			$this->display();
			$this->display();
		}
	}

	//收货地址管理
	public function myaddress(){
		$this->check();
		$Member=M('Member');
		$Addr=M('Member_addr');
		$action=$this->_get('action');
		$map['uname']=$_SESSION['euname'];
		$member=$Member->where($map)->find();

		if($action=='new'){
		}else if($action=='del'){
			$mad['addr_id']=$this->_get('addr_id');
			$Addr->where($mad)->delete();
			echo 1;
		}else{
			$map2['member_id']=$member['member_id'];
			$this->addr=$Addr->where($map2)->order('addr_id desc')->select();

			$this->user=$map['uname'];
			$this->cat=R('Goods/goods_cat');
			$shop_cart=cookie('shopcart');
			$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
			$this->name="会员中心-".$map['uname'];
			$this->display();
		}
	}

	public function login(){
		if(isset($_SESSION['euname'])){
			$this->redirect('../');
		}
	    if($_POST['method']=='login'){
			$Member=M('Member');
			$map['uname|email']=$this->_post('uname');
			$map['password']=md5($this->_post('pwd'));

			$ck=$Member->where($map)->find();
			if($ck){
				session('euname',$ck['uname']);
				$data['last_login']=time();
				$Member->where($map)->save($data);
				$Member->where($map)->setInc('login_count');
				echo "succ";
			}else{
				echo "fail";
			}
		}else{
			$Ads=M('Ads');
			$this->loginad=$Ads->where("place_tag='loginad' and is_show='true'")->order("sorting")->find();
			$this->display();
		}
	}
    
	//交易管理
	public function trade(){
		$this->check();
		$Trade=M('Orders');
		$Member=M('Member');
		$Item=M('Orders_item');
		$Goods=M('Goods');
		$map['uname']=$_SESSION['euname'];
		$member=$Member->where($map)->find();

		$this->goods_hot=$Goods->where("is_post='0'")->order('buy_count desc')->limit(4)->select();
		$this->goods_coup=$Goods->where("goods_label='price' and is_post='0'")->limit(4)->select();
		$this->goods_new=$Goods->where("is_post='0'")->order("uptime desc")->limit(4)->select();
		$this->goods_rec=$Goods->where("goods_label='rec' and is_post='0'")->limit(4)->select();

		$status=$this->_get('status');
		if(empty($status)||$status=='0'){
			$map['pay_status']=array('in','0,1,2,3,4');
		}else{
			switch($status){
				case '1':$map['pay_status']='0';break;//待付款
				case '2':$map['ship_status']='1';break;//已发货
				case '3':$map['pay_status']='1';$map['ship_status']='2';break;//退货中
				case '4':$map['pay_status']='2';$map['ship_status']='3';break;//退款中
				case '5':$map['status']='finish';break;//已完成交易
			}
		}
		$map['member_id']=$member['member_id'];

		import("ORG.Util.Page");
		$count=$Trade->where($map)->order('create_time DESC')->count();
		$Page=new Page($count,10);//C('PAGE_SIZE')每页显示的数量
		//$show=$Page->show();

		$order=$Trade->where($map)->order('create_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();

        $Page->setConfig('header', '条');
		$Page->setConfig('prev', '<img src="__APP__/statics/up.gif">');
		$Page->setConfig('next', '<img src="__APP__/statics/down.gif">');
		$Page->setConfig('theme', "%upPage%  %linkPage% %downPage%  共%totalPage%页");
        $page = $Page->show();

		for($i=0;$i<count($order);$i++){
			$order[$i]['item']=$Item->join('e_goods ON e_orders_item.goods_id=e_goods.goods_id')->where('e_orders_item.order_id='.$order[$i]['order_id'])->select();
		}

		$this->status=$status;
		$this->user=$_SESSION['euname'];
		$this->cat=R('Goods/goods_cat');
		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		$this->order=$order;
		$this->assign("page", $page);
		$this->member=$member;
		$this->name="交易管理";
		$this->display();
	}
    
	//订单详情
	public function order_item(){
		$this->check();
		$map['order_id']=$this->_get('order_id');

		$this->user=$_SESSION['euname'];
		$this->cat=R('Goods/goods_cat');
		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		$this->display();
	}
	//配送信息
	public function viewShip(){
		$Ship=M('Order_ship');
		$Exp=M('Express');
		$Trade=M('Orders');

		$map['order_id']=$this->_get('order');
		$ship=$Ship->where($map)->find();
		$map2['id']=$ship['ex_id'];
		$this->exp=$Exp->where($map2)->find();
		$this->ship=$ship;
		$this->order=$Trade->where($map)->find();
		$this->display();
	}

	public function check_login(){
		$Member=M('Member');
		$map['uname']=$this->_post('uname');
		$map['password']=md5($this->_post('pwd'));

		$ck=$Member->where($map)->find();
		if($ck){
			$_SESSION['euname']=$ck['uname'];
			echo "<script>window.parent.location.reload()</script>";
		}else{
			$this->error("用户名或密码错误！");
		}
	}

	//获取登录会员信息
	public function get_user(){
		$Member=M('Member');
		$map['uname']=$_SESSION['euname'];
		return $Member->where($map)->find();
	}
    
	//弹出登录框
	public function slogin(){
		$this->display();
	}

	public function reg(){
		$Ads=M('Ads');
		$this->regad=$Ads->where("place_tag='Reg' and is_show='true'")->order("sorting")->find();

		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		$this->user=$_SESSION['euname'];
		$this->cat=R('Goods/goods_cat');
		$this->name="用户注册-欢迎您来到蚂蚁街！";
		$this->assign("page", $page);
		$this->display();
	}

	public function ck_reg(){
		$Member=M('Member');
		$map['uname']=$this->_post('uname');
		$map2['email']=$this->_post('email');

		$rname=$Member->where($map)->find();
		$remail=$Member->where($map2)->find();

		if($rname==true) echo '1';
		if($remail) echo '1';
	}

	public function ck_yzm(){
		$yzm=$this->_get('yzm');
		if(session('verify') != md5($yzm)) {
            echo '1';
        }
	}

    //保存注册信息
	public function insert(){
		$Member=M('Member');
		$data['uname']=$this->_post('uname');
		$map['uname']=$this->_post('uname');
		$data['password']=md5($this->_post('pwd'));
		$data['email']=$this->_post('email');
		$map2['email']=$this->_post('email');
		$data['regtime']=time();
		$data['reg_ip']=get_client_ip();
		$data['login_count']=1;
		$data['last_login']=time();

		$yzm=$this->_post('yzm');
		if(session('verify') != md5($yzm)) $this->error("验证码不正确！");

		$rname=$Member->where($map)->find();
		if($rname) $this->error("此用户名已被注册！");
		if(!empty($map2['email'])){
			$remail=$Member->where($map2)->select();
			if($remail) $this->error("此邮箱已被注册！");
		}
		$Member->add($data);
		$_SESSION['euname']=$data['uname'];

        if(C('EMAIL_REG')=='1'){
		    R("Public/smail",array($data['email'],$data['uname'],'欢迎您成为蚂蚁街会员',$other,'reg'));
		}

		$this->redirect('Index/index');
	}

    //退出登录
	public function loginout(){
		session('euname',null); //删除name
		$this->redirect('Index/index');
		//echo "<script language=javascript>history.go(-1).reload();</script>";
	}

    //添加会员地址
	public function add_addr(){
		$Area=M('Area');
		$this->province=$Area->where('area_grade=1')->select();
		$this->display();
	}

	//获取市
	public function get_city(){
		$Area=M('Area');
		$map['parent']=$this->_get('province');
		$next=$Area->where($map)->select();
		echo "<select size='1' name='city'>";
		for($i=0;$i<count($next);$i++){
			echo "<option value='".$next[$i]['area_id']."'>".$next[$i]['local_name']."</option>";	
		}
		echo "</select>";
	}

	//获取区（县）
	public function get_zone(){
		$Area=M('Area');
		$map['parent']=$this->_get('city');
		$next=$Area->where($map)->select();
		echo "<select size='1' name='zone'>";
		for($i=0;$i<count($next);$i++){
			echo "<option value='".$next[$i][local_name]."'>".$next[$i][local_name]."</option>";	
		}
		echo "</select>";
	}

    //会员账户余额
	public function wallet(){
		$this->check();
		$Member=M('Member');
		$Wallet=M('Wallet');
		$map['uname']=$_SESSION['euname'];
		$member=$Member->where($map)->find();
		$action=$this->_get("action");

		switch($action){
			case 'check':
				$map2['wallet_id']=$this->_post('wallet_id');
			    $map2['passcode']=$this->_post('code');
				$map2['status']='active';
				$wallet=$Wallet->where($map2)->find();
				if($wallet){
					if($wallet['end_time']<time()){
					    $wallet['result']=2;
					}else{
						$wallet['result']=1;
					}
					echo json_encode($wallet);
				}else{
					$wallet['result']=0;
					echo json_encode($wallet);
				}
			    break;
			case 'charge':
				$map3['wallet_id']=$this->_post('wallet_id');
			    $map3['passcode']=$this->_post('code');
				$map3['status']='active';
				$map3['end_time']=array('egt',time());
				$wallet=$Wallet->where($map3)->find();
				if($wallet){
					$Mwallet=M('Member_wallet');
					if($Member->where("uname='".$_SESSION['euname']."'")->setInc('wallet',$wallet['amount'])){
						$data['status']='used';
					    if($Wallet->where($map3)->save($data)){
							$data2['member_id']=$member['member_id'];
							$data2['action']=1;
							$data2['money']=$wallet['amount'];
							$data2['op_time']=time();
							$data2['op_name']=$member['uname'];
							$data2['code']=$wallet['passcode'];
							$Mwallet->add($data2);
					        echo 1;
						}
					}
				}else{
					echo 0;
				}
				break;
			default:
				$Mwallet=M('Member_wallet');
				$map2['member_id']=$member['member_id'];
			    $this->wallet=$Mwallet->where($map2)->limit(10)->order('op_time desc')->select();

				$shop_cart=cookie('shopcart');
		        $this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
				$this->member=$member;
				$this->user=$map['uname'];
				$this->cat=R('Goods/goods_cat');
				$this->name="我的账户余额";
				$this->display();
				break;
		}
	}

	//账户金额历史记录
	public function wallet_his(){
		$this->check();
		$Mwallet=M('Member_wallet');
		$Member=M('Member');

		$map['uname']=$_SESSION['euname'];
		$member=$Member->where($map)->find();
		$map2['member_id']=$member['member_id'];
		$count=$Mwallet->where($map2)->count();

		import("ORG.Util.Page");
		$Page=new Page($count,10);

		$wallet=$Mwallet->where($map2)->limit($Page->firstRow.','.$Page->listRows)->order('op_time desc')->select();

        $Page->setConfig('header', '条');
		$Page->setConfig('prev', '<img src="__APP__/statics/up.gif">');
		$Page->setConfig('next', '<img src="__APP__/statics/down.gif">');
		$Page->setConfig('theme', "%upPage%  %linkPage% %downPage%  共%totalPage%页");
        $page = $Page->show();
		for($i=0;$i<count($wallet);$i++){
			$wallet[$i]['code']=implode('-',str_split($wallet[$i]['code'],4));
		}
		$this->wallet=$wallet;
        
		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		$this->user=$_SESSION['euname'];
		$this->cat=R('Goods/goods_cat');
		$this->assign("page", $page);
		$this->name="礼品卡使用记录";
		$this->display();
	}

    //我的优惠券
	public function mycoupon(){
		$this->check();
		$Coupons=M('Coupons');
		$Member=M('Member');

		$map['uname']=$_SESSION['euname'];
		$member=$Member->where($map)->find();
		$map2['owner']=$member['member_id'];
		$count=$Coupons->where($map2)->count();

		import("ORG.Util.Page");
		$Page=new Page($count,10);

		$coupons=$Coupons->where($map2)->limit($Page->firstRow.','.$Page->listRows)->order("end_time")->select();

        $Page->setConfig('header', '条');
		$Page->setConfig('prev', '<img src="__APP__/statics/up.gif">');
		$Page->setConfig('next', '<img src="__APP__/statics/down.gif">');
		$Page->setConfig('theme', "%upPage%  %linkPage% %downPage%  共%totalPage%页");
        $page = $Page->show();
		for($i=0;$i<count($coupons);$i++){
			$coupons[$i]['passcode']=implode('-',str_split($coupons[$i]['passcode'],4));
		}
		$this->coupons=$coupons;
        
		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		$this->user=$_SESSION['euname'];
		$this->cat=R('Goods/goods_cat');
		$this->assign("page", $page);
		$this->name="我的优惠券";
		$this->display();
	}

	public function add_coupons(){
		$this->check();
		$action=$this->_get('action');
		if($action=='save'){
			$Coupons=M('Coupons');
			$Member=M('Member');
			$map['passcode']=$this->_post('code');
			$map['type']='offline';
			$map['status']='active';
			$map['owner']=0;
			$map['end_time']=array('gt',time());
			if($Coupons->where($map)->find()){
				$map2['uname']=$_SESSION['euname'];
				$member=$Member->where($map2)->find();
				$data['type']='online';
				$data['owner']=$member['member_id'];
				if($Coupons->where($map)->save($data)){
					echo 1;
				}else{
					echo 2;
				}
			}else{
				echo 3;
			}
		}else{
			$shop_cart=cookie('shopcart');
			$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
			$this->user=$_SESSION['euname'];
			$this->cat=R('Goods/goods_cat');
			$this->assign("page", $page);
			$this->name="优惠券转换";
			$this->display();
		}
	}

	//收藏夹管理
	public function goodstore(){
		$this->check();
		$Store=M('Member_store');
		$Member=M('Member');
		$map['uname']=$_SESSION['euname'];
		$member=$Member->where($map)->find();

		$action=$this->_get('action');
		if($action=='del'){
			$map3['store_id']=$this->_get('store_id');
			$Store->where($map3)->delete();
			echo 1;
		}else{
			$map2['e_member_store.member_id']=$member['member_id'];
			$count=$Store->where($map2)->count();

			import("ORG.Util.Page");
			$Page=new Page($count,20);

			$this->mylist=$Store->join('e_goods ON e_goods.goods_id=e_member_store.goods_id')->where($map2)->limit($Page->firstRow.','.$Page->listRows)->order('e_member_store.add_time desc')->select();

			$Page->setConfig('header', '条');
			$Page->setConfig('prev', '<img src="__APP__/statics/up.gif">');
			$Page->setConfig('next', '<img src="__APP__/statics/down.gif">');
			$Page->setConfig('theme', "%upPage%  %linkPage% %downPage%  共%totalPage%页");
			$page = $Page->show();
			
			$shop_cart=cookie('shopcart');
			$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
			$this->user=$_SESSION['euname'];
			$this->cat=R('Goods/goods_cat');
			$this->assign("page", $page);
			$this->name="商品收藏";
			$this->display();
		}
	}

}
?>